package com.java1412.goods.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * 测试sentinel的流控和熔断规则
 *
 * @author java1412
 * @since 2024-01-17 10:17:19
 */
@RestController
@RequestMapping("/api/v1/sentinel")
public class SentinelTestController {

    private volatile int total = 0;

    @RequestMapping(value = "/test-sentinel", produces = {"application/json;charset=utf-8"})
    public Object findByGoodsId() {
        return total++;
    }

    @RequestMapping(value = "/test-sentinel-exception", produces = {"application/json;charset=utf-8"})
    public Object testSentinelException() {
        int i = (int) (Math.random() * 100);
        if (i > 10) {
            throw new NullPointerException("随机错误");
        }
        return "成功";
    }
}
